<?php
/**
 * 违规记录控制器
 */
namespace Beerus\Modules\IdealHome\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Beerus\Modules\IdealHome\Actions\ImportVideo;
use Beerus\Modules\IdealHome\Actions\InitVideoData;
use Doraemon\model\Member;
use Doraemon\model\Photos;
use Encore\Admin\Actions\Action;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Grid\Tools;
use Doraemon\model\IdealHomeTab\IdealHomeVideo as mIdealHomeVideo;

class VideoController extends Controller {
    const PAGE_SIZE = 20;

    public function index(Request $request, Content $content) {
        $page = $request->get('page', 1);
        $pageSize = $request->get('per_page', self::PAGE_SIZE);
        $noteId = $request->get('note_id');
        $condition = [];
        if (!empty($noteId)) {
            $condition['note_id[~]'] = $noteId;
        }
        $condition['data_status'] = mIdealHomeVideo::STATUS_NORMAL;
        $total = mIdealHomeVideo::count($condition);
        $condition['ORDER'] = ['id' => 'DESC'];
        $condition['LIMIT'] = [
            ($page - 1) * $pageSize,
            $pageSize
        ];
        $list = mIdealHomeVideo::select('*', $condition);
        $aNoteIds = array_column($list, 'note_id');
        $aUids = array_column($list, 'uid');
        $aMemberList = Member::getUserInfoByIds($aUids);
        $aNickList = array_column($aMemberList, 'nick', 'uid');
        $aNoteList = Photos::getAllList($aNoteIds);
        $aNoteWithRemarkList = array_column($aNoteList, 'remark', 'id');
        $aNoteWithTitleList = array_column($aNoteList, 'title', 'id');
        foreach ($list as &$item) {
            $item['note_title'] = $aNoteWithTitleList[$item['note_id']];
            $item['note_remark'] = $aNoteWithRemarkList[$item['note_id']];
            $item['nick'] = !empty($aNickList[$item['uid']]) ? $aNickList[$item['uid']] : '';
        }
        $gridModel = new pGridDataModel($list, $total, [
            'perPage' => $pageSize
        ]);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();
                $grid->tools(function (Tools $tools) {
                    $tools->append(new ImportVideo());
                });
                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(6, function ($filter) {
                        $filter->equal('note_id', '视频note_id')->placeholder('请输入视频note_id');
                    });
                    $filter->expand();
                });

                $grid->column('id', 'ID')->width(100);
                $grid->column('note_id', '视频noteId')->width(100);
                $grid->column('uid', '用户UID')->width(100);
                $grid->column('nick', '昵称')->width(100);
                $grid->column('note_title', '视频标题')->width(200);
                $grid->column('note_remark', '视频描述')->width(500);
                $grid->disableActions();
            }
        );
        return $content
            ->header('理想家Tab视频模块列表')
            ->row($gridList);
    }
}
